﻿(function ($) {

    function calendarWidget(el, params) {

        var now = new Date();
        var thismonth = params.month;
        var thisyear = params.year;

        var opts = {
            month: thismonth,
            year: thisyear
        };

        $.extend(opts, params);


        var monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
        var dayNames = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        month = i = parseInt(opts.month);
        year = parseInt(opts.year) + 1900;
        var m = 0;
        //TODO:需要将table tr td dom的操作使用jquery方式创建 方便增加对象
        var table = '';

        // next month
        if (month == 11) {
            var next_month = '<a href="?month=' + 1 + '&amp;year=' + (year + 1) + '" title="' + monthNames[0] + ' ' + (year + 1) + '">' + monthNames[0] + ' ' + (year + 1) + '</a>';
        } else {
            var next_month = '<a href="?month=' + (month + 2) + '&amp;year=' + (year) + '" title="' + monthNames[month + 1] + ' ' + (year) + '">' + monthNames[month + 1] + ' ' + (year) + '</a>';
        }

        // previous month
        if (month == 0) {
            var prev_month = '<a href="?month=' + 12 + '&amp;year=' + (year - 1) + '" title="' + monthNames[11] + ' ' + (year - 1) + '">' + monthNames[11] + ' ' + (year - 1) + '</a>';
        } else {
            var prev_month = '<a href="?month=' + (month) + '&amp;year=' + (year) + '" title="' + monthNames[month - 1] + ' ' + (year) + '">' + monthNames[month - 1] + ' ' + (year) + '</a>';
        }

        table += ('<h3 id="current-month">' + monthNames[month] + ' ' + year + '</h3>');
        // uncomment the following lines if you'd like to display calendar month based on 'month' and 'view' paramaters from the URL
        //table += ('<div class="nav-prev">'+ prev_month +'</div>');
        //table += ('<div class="nav-next">'+ next_month +'</div>');
        table += ('<table class="calendar-month " ' + 'id="calendar-month' + i + ' " cellspacing="0">');

        table += '<tr>';

        for (d = 0; d < 7; d++) {
            table += '<th class="weekday">' + dayNames[d] + '</th>';
        }

        table += '</tr>';

        var days = getDaysInMonth(month, year);
        var firstDayDate = new Date(year, month, 1);
        var firstDay = firstDayDate.getDay();

        var prev_days = getDaysInMonth(month, year);
        var firstDayDate = new Date(year, month, 1);
        var firstDay = firstDayDate.getDay();

        var prev_m = month == 0 ? 11 : month - 1;
        var prev_y = prev_m == 11 ? year - 1 : year;
        var prev_days = getDaysInMonth(prev_m, prev_y);
        firstDay = (firstDay == 0 && firstDayDate) ? 7 : firstDay;

        var i = 0;
        for (j = 0; j < 42; j++) {

            if ((j < firstDay)) {
                table += ('<td class="other-month"><span class="day">' + (prev_days - firstDay + j + 1) + '</span></td>');
            } else if ((j >= firstDay + getDaysInMonth(month, year))) {
                i = i + 1;
                table += ('<td class="other-month"><span class="day">' + i + '</span></td>');
            } else {
                table += ('<td class="current-month day' + (j - firstDay + 1) + '"><span class="day">' + (j - firstDay + 1) + '</span></td>');
            }
            if (j % 7 == 6) table += ('</tr>');
        }

        table += ('</table>');

        el.html(table);

        $(function () {

            var routePriceModel = {
                adult_price: 0,
                children_price: 0,
                total_numbers: 0,
                sale_numbers: 0,
                route_date: ''
            };

            //TODO:判断是否为管理员界面，如果是管理员界面 则需要显示可编辑金额，如果非管理员界面则不需要编辑
            if (params.isAdmin) {
                debugger;
                $('td.current-month>span', el).each(function () {
                    var day = $(this).text();
                    $(this).empty();
                    var a = $('<a>').attr('href', 'javascript:;').text(day).appendTo($(this));
                    a.data('date', year + "-" + month + "-" + day);
                    a.on('click', function () {
                        var dialog = $("<div>").dialog({
                            resizable: false,
                            height: 140,
                            modal: true,
                            buttons: {
                                "确定": function () {
                                    $(this).dialog("close");
                                },
                                "取消": function () {
                                    $(this).dialog("close");
                                    dialog.remove();
                                }
                            }
                        });
                        var panel = $('<table>').appendTo(dialog);
                        var adultPrice = $('<tr>').appendTo(panel);
                        var childrenPrice = $('<tr>').appendTo(panel);
                        var totalNumbers = $('<tr>').appendTo(panel);
                        var adultePriceText = $('<input type="text"/>');
                        var totalNumbersText = $('<input type="text"/>');
                        var childrenPriceTexe = $('<input type="text"/>');
                        totalNumbers.append($('<td>').text('总票数')).append($('<td>').append(totalNumbersText));
                        adultPrice.append($('<td>').text('成人价格')).append($('<td>').append(adultePriceText));
                        childrenPrice.append($('<td>').text('儿童价格')).append($('<td>').append(childrenPriceTexe));
                        dialog.html();
                    });
                });
            } else {
            }
        });
    }

    function getDaysInMonth(month, year) {
        var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if ((month == 1) && (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
            return 29;
        } else {
            return daysInMonth[month];
        }
    }


    // jQuery plugin initialisation
    $.fn.calendarWidget = function (params) {
        calendarWidget(this, params);
        return this;
    };

})(jQuery);
